Easy2Siksha.com
GNDU QUESTION PAPERS 2025
Bachelor of Computer Applicaon (BCA) 2nd Semester
(Batch 2023-26) (CBGS)
INTRODUCTION TO PROGRAMMING C++
Time Allowed: 3 Hours Maximum Marks: 75
Note: Aempt Five quesons in all, selecng at least One queson from each secon. The
Fih queson may be aempted from any secon. All quesons carry equal marks.
SECTION-A
1. Describe the advantages and disadvantages of tradional programming
2. What is an abstract class ? How do we create one?
SECTION-B
3. What are the salient OOP features of C++?
4. Explain constructor types with C++ code snippet.
SECTION-C
5. Explain the challenges of funcon overloading
6. What is type conversion ? How is it done?
Easy2Siksha.com
SECTION-D
7 Explain inheritance with single C++ example
8. How does the friend funcon work in C++?
GNDU ANSWER PAPERS 2025
Bachelor of Computer Applicaon (BCA) 2nd Semester
(Batch 2023-26) (CBGS)
INTRODUCTION TO PROGRAMMING C++
Time Allowed: 3 Hours Maximum Marks: 75
Note: Aempt Five quesons in all, selecng at least One queson from each secon. The
Fih queson may be aempted from any secon. All quesons carry equal marks.
SECTION-A
1. Describe the advantages and disadvantages of tradional programming
Ans: Imagine you are giving instructions to someone step-by-step to cook a meal. You tell
them exactly what to do: first cut vegetables, then heat oil, then add spices, and so on. This
is very similar to traditional programmingyou give the computer clear, step-by-step
instructions to perform a task.
Traditional programming refers to the classical way of coding where a programmer writes
explicit logic using languages like C, C++, or Java. The computer simply follows those
instructions exactly as written.
󼩏󼩐󼩑 How Traditional Programming Works
Easy2Siksha.com
Here’s a simple diagram to understand:
Problem
Programmer
Writes Logic (Code)
Computer
Output
In traditional programming:
The programmer thinks and writes logic
The computer executes it
The result depends fully on the instructions
󷄧󼿒 Advantages of Traditional Programming
Let’s explore the benefits with simple explanations and real-life connections.
1. Full Control Over the Program
In traditional programming, the programmer controls everything.
󷷑󷷒󷷓󷷔 Example:
If you write a program to calculate marks, you decide:
How marks are entered
How they are calculated
What output is shown
󽆤 This gives precision and accuracy.
2. Easy to Understand (for Logical Tasks)
For simple tasks like calculations, sorting, or printing results, traditional programming is very
clear.
󷷑󷷒󷷓󷷔 Example:
Adding two numbers:
Easy2Siksha.com
Input number A
Input number B
Output A + B
󽆤 Very straightforward and easy to debug.
3. Reliable and Predictable
Since everything is predefined, the output is always the same for the same input.
󷷑󷷒󷷓󷷔 Example:
If you run a calculator program, it will always give the same result for 2 + 2.
󽆤 No surprisesvery stable and consistent.
4. Better Performance
Traditional programs are often faster because:
No unnecessary processing
Direct instructions
󷷑󷷒󷷓󷷔 Example:
System-level software like operating systems or banking systems often use traditional
programming for speed.
5. Easier Debugging
If something goes wrong, you can trace it step-by-step.
󷷑󷷒󷷓󷷔 Example:
If a program gives wrong output, you check each line to find the mistake.
󽆤 Problems are easier to locate and fix.
󽆱 Disadvantages of Traditional Programming
Now let’s understand the limitations in a simple way.
Easy2Siksha.com
1. Requires Deep Logical Thinking
The programmer must write every step.
󷷑󷷒󷷓󷷔 Example:
If you want to build a program that recognizes faces, you must define:
What is a face
How to detect eyes, nose, etc.
󺆅󺇗󺇚󺇘󺇙 This becomes very difficult for complex problems.
2. Not Flexible or Adaptive
Traditional programs cannot learn or adapt on their own.
󷷑󷷒󷷓󷷔 Example:
A spam filter written using traditional programming:
Cannot improve unless manually updated
󽆱 No learning from new data.
3. Time-Consuming
Writing detailed logic for everything takes time.
󷷑󷷒󷷓󷷔 Example:
Creating a large system like an e-commerce website requires thousands of lines of code.
󼾗󼾘󼾛󼾜󼾙󼾚 Development becomes slow.
4. Difficult for Complex Problems
Some problems are too complex to define step-by-step.
󷷑󷷒󷷓󷷔 Example:
Speech recognition
Image recognition
Self-driving cars
Easy2Siksha.com
󽆱 Traditional programming struggles here.
5. Maintenance is Hard
Updating the program requires manual changes.
󷷑󷷒󷷓󷷔 Example:
If rules change in a tax system, the programmer must update the code.
󹻯 Continuous effort is required.
󷘹󷘴󷘵󷘶󷘷󷘸 Final Understanding
Traditional programming is like a strict teacher:
It follows rules exactly
It does not learn new things
It needs clear instructions
󷷑󷷒󷷓󷷔 It is best for simple, structured, and predictable tasks
󷷑󷷒󷷓󷷔 But it struggles with complex, real-world problems
󼫹󼫺 Conclusion
Traditional programming has been the foundation of computer science for many years. It is
powerful, reliable, and efficient when used in the right situations. However, as problems
become more complexlike understanding human language or recognizing imagesthis
approach shows its limitations.
That’s why modern technologies like Artificial Intelligence and Machine Learning are
becoming popularthey allow systems to learn instead of being told everything step-by-
step.
Easy2Siksha.com
2. What is an abstract class ? How do we create one?
Ans: 󷊆󷊇 What Is an Abstract Class?
In object-oriented programming (OOP), an abstract class is a class that cannot be
instantiated directly. In other words, you cannot create objects from it. Instead, it serves as
a blueprint for other classes.
An abstract class usually contains at least one pure virtual functiona function that has no
body and must be implemented by derived (child) classes.
Think of it like a contract: the abstract class says, “Any class that inherits from me must
provide its own version of these functions.”
󷘧󷘨 Everyday Analogy
Imagine a school. There’s a general concept of a Teacher. But you don’t hire a “generic
teacher”—you hire a Math Teacher, Science Teacher, or English Teacher.
The abstract class is like the idea of “Teacher.” It defines what all teachers must be able to
do (teach, give assignments, grade students), but it doesn’t specify how. Each subject
teacher (derived class) fills in the details.
So, the abstract class sets the rules, and the child classes provide the actual implementation.
󼩺󼩻 Why Do We Need Abstract Classes?
Abstract classes are important because they:
1. Provide a blueprint: They define what functions must exist in derived classes.
2. Encourage consistency: All child classes follow the same structure.
3. Support polymorphism: You can use pointers or references to the abstract class to
work with different derived classes in a uniform way.
4. Separate design from implementation: The abstract class focuses on “what” needs
to be done, while derived classes focus on “how” it’s done.
󹶓󹶔󹶕󹶖󹶗󹶘 How Do We Create an Abstract Class in C++?
In C++, an abstract class is created by declaring at least one pure virtual function.
A pure virtual function is written like this:
virtual void functionName() = 0;
The = 0 means the function has no bodyit’s just a declaration.
󺛺󺛻󺛿󺜀󺛼󺛽󺛾 Example: Abstract Class in C++
Easy2Siksha.com
Let’s create an abstract class called Shape.
#include <iostream>
using namespace std;
// Abstract class
class Shape {
public:
// Pure virtual function
virtual void draw() = 0;
// Normal function (optional)
void info() {
cout << "This is a shape." << endl;
}
};
// Derived class: Circle
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle." << endl;
}
};
// Derived class: Rectangle
class Rectangle : public Shape {
public:
void draw() override {
cout << "Drawing a Rectangle." << endl;
}
};
int main() {
// Shape s; // 󽆱 Error: cannot create object of abstract
class
Shape* shape1 = new Circle();
Shape* shape2 = new Rectangle();
shape1->draw(); // Output: Drawing a Circle.
shape2->draw(); // Output: Drawing a Rectangle.
delete shape1;
delete shape2;
return 0;
}
󷗿󷘀󷘁󷘂󷘃 Diagram Explanation
Easy2Siksha.com
Think of it like this:
Abstract Class: Shape
|
|---> Circle (implements draw())
|
|---> Rectangle (implements draw())
Shape defines the rule: “Every shape must be able to draw itself.”
Circle and Rectangle provide their own versions of draw().
You cannot create a generic Shape object, but you can create Circle or Rectangle.
󷘧󷘨 Relatable Story
Imagine you’re designing a video game. You want different characters: Warrior, Archer,
Mage. All characters must be able to attack, but the way they attack is different.
Warrior uses a sword.
Archer uses a bow.
Mage uses magic.
You create an abstract class Character with a pure virtual function attack(). Each derived
class (Warrior, Archer, Mage) implements attack() in its own way.
This ensures consistency (all characters can attack) while allowing flexibility (different attack
styles).
󼩏󼩐󼩑 Practice Exercise
Try creating an abstract class Animal with a pure virtual function sound().
Derived class Dog should print “Woof.”
Derived class Cat should print “Meow.”
This exercise will help you see how abstract classes enforce rules while allowing variety.
󷈷󷈸󷈹󷈺󷈻󷈼 Key Points to Remember
An abstract class cannot be instantiated.
It must have at least one pure virtual function.
Derived classes must implement pure virtual functions.
Abstract classes are used to define interfaces or blueprints.
󹵙󹵚󹵛󹵜 Conclusion
An abstract class in C++ is a class that serves as a blueprint and cannot be directly
instantiated. It contains pure virtual functions that derived classes must implement.
Easy2Siksha.com
Abstract classes are essential for enforcing consistency, supporting polymorphism, and
separating design from implementation.
For example, the abstract class Shape defines a pure virtual function draw(). Derived classes
like Circle and Rectangle implement draw() in their own way. This ensures all shapes can be
drawn, but each has its unique behavior.
In short, abstract classes are the backbone of structured object-oriented design. They define
the “what” while leaving the “how” to child classes, making programs more organized,
flexible, and powerful.
SECTION-B
3. What are the salient OOP features of C++?
Ans: Imagine you are building a real-world system, like a college management system. You
have students, teachers, subjects, marks, etc. Instead of writing everything in one messy
block of code, OOP helps you organize your program just like real lifeby grouping related
things together.
C++ supports OOP to make programs easy to understand, reusable, secure, and flexible.
󹼥 1. Class and Object
󼩏󼩐󼩑 Idea:
A class is like a blueprint, and an object is the real thing created from that blueprint.
󹵙󹵚󹵛󹵜 Example:
Class → “Car design”
Object → “My actual car”
󹳾󹳿󹴀󹴁󹴂󹴃 In C++:
class Student {
public:
string name;
int age;
};
Here, Student is a class.
Easy2Siksha.com
Student s1;
Now s1 is an object.
󹵍󹵉󹵎󹵏󹵐 Diagram:
󹼥 2. Encapsulation (Data Hiding)
󼩏󼩐󼩑 Idea:
Encapsulation means wrapping data and functions together and controlling access.
Think of it like a capsule 󹨋󹨌󹨍everything is packed inside.
󹵙󹵚󹵛󹵜 Real-life Example:
Easy2Siksha.com
ATM machine:
You can withdraw money
But you cannot see internal code or process
󹳾󹳿󹴀󹴁󹴂󹴃 In C++:
class Bank {
private:
int balance;
public:
void setBalance(int b) {
balance = b;
}
int getBalance() {
return balance;
}
};
Here, balance is hidden (private), and accessed using functions.
󹼥 3. Abstraction (Hiding Complexity)
󼩏󼩐󼩑 Idea:
Abstraction means show only important details and hide unnecessary ones.
󹵙󹵚󹵛󹵜 Real-life Example:
Driving a car:
You use steering, brake, accelerator
You don’t need to know engine mechanics
󹳾󹳿󹴀󹴁󹴂󹴃 In C++:
class Car {
public:
void drive() {
cout << "Car is driving";
}
};
User only sees drive() function, not internal working.
Easy2Siksha.com
󹵍󹵉󹵎󹵏󹵐 Diagram:
Easy2Siksha.com
󹼥 4. Inheritance (Reusability)
󼩏󼩐󼩑 Idea:
Inheritance allows one class to reuse properties of another class.
󹵙󹵚󹵛󹵜 Real-life Example:
Child inherits features from parents
󹳾󹳿󹴀󹴁󹴂󹴃 In C++:
class Animal {
public:
void eat() {
cout << "Eating...";
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking...";
}
};
Here, Dog inherits Animal.
󹵍󹵉󹵎󹵏󹵐 Diagram:
Easy2Siksha.com
󹼥 5. Polymorphism (Many Forms)
󼩏󼩐󼩑 Idea:
Polymorphism means one function behaves differently in different situations.
󹵙󹵚󹵛󹵜 Types:
1. Compile-time (Function Overloading)
2. Run-time (Function Overriding)
󹵙󹵚󹵛󹵜 Example 1: Function Overloading
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
Same function name, different parameters.
󹵙󹵚󹵛󹵜 Example 2: Function Overriding
Easy2Siksha.com
class Animal {
public:
void sound() {
cout << "Animal sound";
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks";
}
};
Same function, different behavior.
󹼥 6. Dynamic Binding (Late Binding)
󼩏󼩐󼩑 Idea:
The function call is decided at runtime, not compile time.
󹵙󹵚󹵛󹵜 Simple Meaning:
Program decides which function to run during execution.
Used with virtual functions.
󹼥 7. Message Passing
󼩏󼩐󼩑 Idea:
Objects communicate with each other using messages (function calls).
󹵙󹵚󹵛󹵜 Example:
s1.setBalance(5000);
Here, object s1 is sending a message to function setBalance().
󷘹󷘴󷘵󷘶󷘷󷘸 Final Summary (Easy to Remember)
Easy2Siksha.com
Feature
Meaning (Simple Words)
Class & Object
Blueprint & real item
Encapsulation
Data hiding
Abstraction
Hide complexity
Inheritance
Reuse code
Polymorphism
Many forms
Dynamic Binding
Decide at runtime
Message Passing
Objects communicate
4. Explain constructor types with C++ code snippet.
Ans: 󷊆󷊇 What Is a Constructor?
In C++, a constructor is a special member function of a class that is automatically called
when an object of that class is created. Its purpose is to initialize the objectthat is, to set
up its initial values or perform any setup tasks.
Key points:
A constructor has the same name as the class.
It has no return type (not even void).
It is automatically invoked when an object is created.
Think of a constructor as the “welcome function” of a class—it prepares the object for use.
󷘧󷘨 Everyday Analogy
Imagine you buy a new smartphone. When you switch it on for the first time, it
automatically sets up language, date, and time. That’s like a constructor—it initializes the
phone so you can start using it.
Without constructors, you’d have to manually set everything up each time, which would be
tedious.
󼩺󼩻 Types of Constructors in C++
There are several types of constructors in C++:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Dynamic Constructor (sometimes discussed as a variation)
5. Constructor Overloading (when multiple constructors exist in the same class)
Let’s go through each one with examples.
Easy2Siksha.com
󹶓󹶔󹶕󹶖󹶗󹶘 1. Default Constructor
A default constructor takes no arguments. It initializes objects with default values.
#include <iostream>
using namespace std;
class Student {
public:
int id;
string name;
// Default constructor
Student() {
id = 0;
name = "Unknown";
}
void display() {
cout << "ID: " << id << ", Name: " << name << endl;
}
};
int main() {
Student s1; // Default constructor is called
s1.display(); // Output: ID: 0, Name: Unknown
return 0;
}
Here, when s1 is created, the default constructor sets id = 0 and name = "Unknown".
󹶓󹶔󹶕󹶖󹶗󹶘 2. Parameterized Constructor
A parameterized constructor takes arguments to initialize objects with specific values.
class Student {
public:
int id;
string name;
// Parameterized constructor
Student(int i, string n) {
id = i;
name = n;
}
void display() {
cout << "ID: " << id << ", Name: " << name << endl;
}
};
Easy2Siksha.com
int main() {
Student s1(101, "Aman"); // Parameterized constructor is
called
s1.display(); // Output: ID: 101, Name: Aman
return 0;
}
This allows you to create objects with customized values right at the start.
󹶓󹶔󹶕󹶖󹶗󹶘 3. Copy Constructor
A copy constructor creates a new object as a copy of an existing object.
class Student {
public:
int id;
string name;
// Parameterized constructor
Student(int i, string n) {
id = i;
name = n;
}
// Copy constructor
Student(const Student &s) {
id = s.id;
name = s.name;
}
void display() {
cout << "ID: " << id << ", Name: " << name << endl;
}
};
int main() {
Student s1(101, "Aman");
Student s2 = s1; // Copy constructor is called
s2.display(); // Output: ID: 101, Name: Aman
return 0;
}
This is useful when you want to duplicate objects safely.
󹶓󹶔󹶕󹶖󹶗󹶘 4. Dynamic Constructor
A dynamic constructor uses dynamic memory allocation (like new) to initialize objects.
class Student {
Easy2Siksha.com
public:
int *marks;
// Dynamic constructor
Student(int m) {
marks = new int;
*marks = m;
}
void display() {
cout << "Marks: " << *marks << endl;
}
~Student() { // Destructor to free memory
delete marks;
}
};
int main() {
Student s1(95); // Dynamic constructor allocates memory
s1.display(); // Output: Marks: 95
return 0;
}
This is useful when objects need memory allocated at runtime.
󹶓󹶔󹶕󹶖󹶗󹶘 5. Constructor Overloading
You can have multiple constructors in the same class with different parameter lists. This is
called constructor overloading.
cpp
class Student {
public:
int id;
string name;
// Default constructor
Student() {
id = 0;
name = "Unknown";
}
// Parameterized constructor
Student(int i, string n) {
id = i;
name = n;
}
void display() {
Easy2Siksha.com
cout << "ID: " << id << ", Name: " << name << endl;
}
};
int main() {
Student s1; // Calls default constructor
Student s2(102, "Ravi"); // Calls parameterized
constructor
s1.display(); // Output: ID: 0, Name: Unknown
s2.display(); // Output: ID: 102, Name: Ravi
return 0;
}
This gives flexibility in object creation.
󷗿󷘀󷘁󷘂󷘃 Diagram Explanation
Think of constructors as different “doors” to enter a class:
Class: Student
|
|---> Default Constructor (no arguments)
|
|---> Parameterized Constructor (custom values)
|
|---> Copy Constructor (duplicate object)
|
|---> Dynamic Constructor (allocate memory)
Each constructor provides a different way to initialize the object, but they all serve the same
purpose: preparing the object for use.
󷘧󷘨 Relatable Story
Imagine you’re opening a bank account.
If you don’t provide details, the bank gives you a default account (Default
Constructor).
If you provide your name and ID, the bank sets up a personalized account
(Parameterized Constructor).
If you want another account exactly like your first one, the bank copies it (Copy
Constructor).
Easy2Siksha.com
If you need special features that require extra resources, the bank allocates them
dynamically (Dynamic Constructor).
This shows how constructors give flexibility in object creation.
󹵙󹵚󹵛󹵜 Conclusion
Constructors in C++ are special functions that initialize objects when they are created. The
main types are:
Default Constructor: Initializes with default values.
Parameterized Constructor: Initializes with specific values.
Copy Constructor: Creates a copy of an existing object.
Dynamic Constructor: Allocates memory dynamically.
Constructor Overloading: Multiple constructors for flexibility.
In essence, constructors are the “entry points” of a class. They ensure that objects start life
in a valid, predictable state. By mastering constructors, you gain control over how objects
are created, making your programs more reliable and flexible.
SECTION-C
5. Explain the challenges of funcon overloading
Ans: Challenges of Function Overloading
Before understanding the challenges, let’s quickly recall what function overloading means.
󷷑󷷒󷷓󷷔 Function overloading is a feature (in languages like C++, Java, etc.) where multiple
functions can have the same name but different parameters (different number, type, or
order of arguments).
󹵍󹵉󹵎󹵏󹵐 Simple Visual Idea of Function Overloading
Easy2Siksha.com
4
Think of it like this:
Same function name → add()
But different inputs:
o add(int a, int b)
o add(double a, double b)
o add(int a, int b, int c)
󷷑󷷒󷷓󷷔 The computer decides which one to use based on the arguments.
Sounds simple, right? But here’s where the challenges start.
󽁔󽁕󽁖 Challenges of Function Overloading
Let’s understand them one by one in a very easy and relatable way.
1. 󺽈󺽒󺽓󺽉󺽊󺽋󺽌󺽍󺽎󺽏󺽐󺽑󺽔󺽕󺽖 Confusion for Beginners
For students or beginners, function overloading can feel confusing.
󷷑󷷒󷷓󷷔 Why?
Because:
Same function name is used again and again.
Only parameters are different.
󹲉󹲊󹲋󹲌󹲍 Example:
Easy2Siksha.com
print(int a);
print(double a);
print(string a);
A beginner might think:
“How does the program know which print() to call?”
󷷑󷷒󷷓󷷔 This creates mental confusion, especially when learning programming for the first time.
2. 󽀼󽀽󽁀󽁁󽀾󽁂󽀿󽁃 Ambiguity Problem (Biggest Issue)
This is one of the most important challenges.
󷷑󷷒󷷓󷷔 Ambiguity means:
The compiler cannot decide which function to call.
󹲉󹲊󹲋󹲌󹲍 Example:
void fun(int a, double b);
void fun(double a, int b);
fun(10, 20);
󷷑󷷒󷷓󷷔 Now the compiler gets confused:
Should 10 be treated as int or double?
Which function should be called?
󽆱 Result: Compilation Error (Ambiguity Error)
󷷑󷷒󷷓󷷔 This is dangerous because even small mistakes can break your program.
3. 󼩏󼩐󼩑 Harder Code Understanding
When many overloaded functions exist, reading the code becomes difficult.
󹲉󹲊󹲋󹲌󹲍 Example:
calculate(int a);
calculate(int a, int b);
Easy2Siksha.com
calculate(double a);
calculate(int a, double b);
󷷑󷷒󷷓󷷔 For a new programmer or even a teammate:
It becomes hard to understand which function is being used.
Debugging becomes confusing.
󹵙󹵚󹵛󹵜 Real-life analogy:
It’s like calling someone named “Rahul” in a room where 5 people have the same name.
4. 󷱲󷱳󷱴󷱵󷱶󷱷󷱸󷱾󷱹󷱺󷱻󷱼󷱽 Debugging Becomes Difficult
When errors occur, it is harder to identify:
Which overloaded function caused the issue?
Which version is being executed?
󹲉󹲊󹲋󹲌󹲍 Example:
If output is wrong:
Is it add(int, int)?
Or add(double, double)?
󷷑󷷒󷷓󷷔 You need to carefully check parameters and types.
5. 󽁗 Performance Overhead (Minor but Important)
The compiler has to:
Analyze all overloaded functions
Match arguments
Select the correct one
󷷑󷷒󷷓󷷔 This process adds a small overhead.
While modern systems handle it well, in large programs:
It may slightly affect performance.
6. 󷄧󹹯󹹰 Type Conversion Issues
Easy2Siksha.com
Sometimes, the compiler automatically converts data types.
󷷑󷷒󷷓󷷔 This can lead to unexpected results.
󹲉󹲊󹲋󹲌󹲍 Example:
void fun(int a);
void fun(double a);
fun(5.5);
󷷑󷷒󷷓󷷔 Which one will run?
Ideally → fun(double)
But sometimes conversion rules may confuse behavior.
󷷑󷷒󷷓󷷔 This can lead to:
Wrong function being called
Unexpected outputs
7. 󼩺󼩻 Limited by Return Type
You cannot overload functions only by changing return type.
󽆱 This is NOT allowed:
int sum(int a, int b);
double sum(int a, int b);
󷷑󷷒󷷓󷷔 Compiler cannot differentiate based only on return type.
󹵙󹵚󹵛󹵜 So developers must carefully design parameters.
8. 󹴈󼪩󼪪󼪫󼪬󼪱󼪲󼪭󼪮󼪯󼪰 Maintenance Becomes Tough
In large projects:
Too many overloaded functions
Similar names everywhere
󷷑󷷒󷷓󷷔 Makes code:
Easy2Siksha.com
Hard to maintain
Hard to update
Easy to break
󹲉󹲊󹲋󹲌󹲍 Example:
If you change one version of a function:
You may accidentally affect others.
9. 󹶜󹶟󹶝󹶞󹶠󹶡󹶢󹶣󹶤󹶥󹶦󹶧 Overuse Leads to Bad Design
Function overloading is powerful, but:
󷷑󷷒󷷓󷷔 Overusing it can make code messy.
Instead of:
process(int a);
process(double a);
process(string a);
Sometimes it's better to:
Use different meaningful names
󷷑󷷒󷷓󷷔 Clean code is always better than clever code.
󷘹󷘴󷘵󷘶󷘷󷘸 Final Summary (Easy to Remember)
Function overloading is useful but comes with challenges:
Challenge
Simple Meaning
Confusion
Same name, different behavior
Ambiguity
Compiler cannot decide
Hard to read
Code becomes complex
Debugging issues
Difficult to trace errors
Type conversion problems
Wrong function may run
Maintenance issues
Hard in big programs
󼩏󼩐󼩑 Final Thought
Function overloading is like having multiple tools with the same name but different shapes.
Easy2Siksha.com
󷷑󷷒󷷓󷷔 It’s helpful…
BUT…
󽁔󽁕󽁖 If youre not careful:
You may pick the wrong tool
Or not know which one to use
6. What is type conversion ? How is it done?
Ans: 󷊆󷊇 What Is Type Conversion?
In programming, type conversion means changing a value from one data type to another.
For example, converting an integer (int) into a floating-point number (float), or a character
(char) into its ASCII integer value.
Why do we need this? Because sometimes different parts of a program use different data
types, and they need to work together. Type conversion ensures compatibility and prevents
errors.
󷘧󷘨 Everyday Analogy
Imagine you’re traveling abroad. You have money in Indian Rupees, but the shopkeeper
accepts only US Dollars. You need to convert your money into dollars to make the purchase.
Similarly, in programming, when one function expects a float but you provide an int, the
compiler converts the type so they can “talk” to each other.
󼩺󼩻 Types of Type Conversion in C++
There are two main categories:
1. Implicit Type Conversion (Type Casting / Type Promotion)
o Done automatically by the compiler.
o Also called type promotion.
o Example: converting int to float when needed.
2. Explicit Type Conversion (Type Casting by Programmer)
o Done manually by the programmer.
o Also called type casting.
o Example: forcing a float into an int.
󹶓󹶔󹶕󹶖󹶗󹶘 1. Implicit Type Conversion
Easy2Siksha.com
This happens automatically when you mix different data types in an expression. The
compiler promotes smaller types into larger types to avoid loss of data.
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10;
float b = 5.5;
float result = a + b; // int 'a' is converted to float
automatically
cout << "Result: " << result << endl; // Output: 15.5
return 0;
}
Here, a (int) is automatically converted into float before addition. This prevents loss of
precision.
󹶓󹶔󹶕󹶖󹶗󹶘 2. Explicit Type Conversion
This is when the programmer manually converts one type into another. There are different
ways to do this in C++:
(a) Using Cast Operators
#include <iostream>
using namespace std;
int main() {
float pi = 3.14159;
int approx = (int)pi; // Explicit conversion
cout << "Approximate value: " << approx << endl; //
Output: 3
return 0;
}
Here, (int)pi forces the float into an integer, dropping the decimal part.
(b) Using C++ Style Casts
C++ provides specific cast operators:
static_cast<Type>(expression)
dynamic_cast<Type>(expression)
const_cast<Type>(expression)
reinterpret_cast<Type>(expression)
Easy2Siksha.com
Example:
#include <iostream>
using namespace std;
int main() {
double x = 9.99;
int y = static_cast<int>(x); // Explicit conversion
cout << "Converted value: " << y << endl; // Output: 9
return 0;
}
󺛺󺛻󺛿󺜀󺛼󺛽󺛾 Special Cases of Type Conversion
1. Numeric Promotion Smaller types (char, short) are promoted to int when used in
arithmetic.
2. Conversion Between Classes In object-oriented programming, you can define
conversion functions to change one class type into another.
3. String to Number Conversion Using functions like stoi() (string to integer) or stof()
(string to float).
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "123";
int num = stoi(s); // Convert string to int
cout << "Number: " << num << endl; // Output: 123
return 0;
}
󷗿󷘀󷘁󷘂󷘃 Diagram Explanation
Think of type conversion as a flow:
Data Type A ----> Converted ----> Data Type B
For example:
Code
int (10) ----> float (10.0)
float (3.14) ----> int (3)
char ('A') ----> int (65)
󷘧󷘨 Relatable Story
Easy2Siksha.com
Imagine you’re baking a cake. The recipe says “2 cups of flour,” but your measuring tool is in
grams. You need to convert cups into grams to follow the recipe correctly.
In programming, type conversion is like that—it ensures values are in the right “unit” so the
program runs smoothly.
󼩏󼩐󼩑 Practice Exercise
Try writing a program that:
1. Takes an integer input.
2. Converts it into a float.
3. Converts it back into a string.
This exercise will help you see how type conversion works across different data types.
󷈷󷈸󷈹󷈺󷈻󷈼 Why Type Conversion Matters
Prevents errors when mixing data types.
Ensures precision in calculations.
Allows flexibility in programming.
Supports polymorphism and object-oriented design.
Without type conversion, programs would crash or give incorrect results whenever different
data types interact.
󹵙󹵚󹵛󹵜 Conclusion
Type conversion in C++ is the process of changing a value from one data type to another. It
can be implicit (done automatically by the compiler) or explicit (done manually by the
programmer using casting).
Examples include converting integers to floats, floats to integers, characters to ASCII values,
or strings to numbers. Type conversion ensures compatibility, prevents errors, and makes
programs more flexible and reliable.
In short, type conversion is like a translator in programmingit makes sure different
“languages” (data types) can work together smoothly. By mastering type conversion, you
gain control over how values are handled, making your programs more accurate and
efficient.
Easy2Siksha.com
SECTION-D
7 Explain inheritance with single C++ example
Ans: Think about a parent and child relationship. A child inherits qualities like eye color,
height, habits, etc., from their parents. Similarly, in programming, a class can inherit
properties and behaviors from another class.
This concept is called Inheritance in C++.
󹼧 What is Inheritance?
Inheritance is a feature of Object-Oriented Programming (OOP) where:
One class (called base class or parent class) shares its data and functions
Another class (called derived class or child class) receives those features
󷷑󷷒󷷓󷷔 This helps in:
Code reuse (no need to write same code again)
Better organization
Easy maintenance
󹼧 Simple Definition
󷷑󷷒󷷓󷷔 Inheritance is the process by which one class acquires the properties and functions of
another class.
󹼧 Types of Inheritance (Quick Idea)
There are multiple types, but here we focus on Single Inheritance:
Single Inheritance → One base class → One derived class
Example:
Animal → Dog
󹼧 Real-Life Analogy
Let’s say:
Easy2Siksha.com
Animal → can eat, sleep
Dog → is also an animal, so it can eat and sleep + it can bark
So, instead of writing eat/sleep again in Dog class, we inherit them from Animal.
󹼧 Structure of Inheritance in C++
Basic syntax:
class DerivedClass : access_specifier BaseClass
{
// additional members
};
Example:
class Dog : public Animal
󹼧 Single Inheritance Example (C++)
Let’s understand with a simple program 󷶹󷶻󷶼󷶽󷶺
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "Animal is eating" << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking" << endl;
}
};
int main() {
Dog d;
d.eat(); // inherited from Animal
d.bark(); // own function
Easy2Siksha.com
return 0;
}
󹼧 Output
Animal is eating
Dog is barking
󹼧 Explanation of the Code
Let’s break it step by step in a very simple way:
1. Base Class (Animal)
class Animal {
public:
void eat()
This class has one function: eat()
It represents common behavior
2. Derived Class (Dog)
class Dog : public Animal
Dog inherits from Animal
public means inherited members stay public
Dog has:
eat() → from Animal
bark() → its own function
3. Main Function
Dog d;
We create an object of Dog
d.eat();
Easy2Siksha.com
Even though eat() is not written in Dog, it works because of inheritance
d.bark();
This is Dog’s own function
󹼧 Diagram Explanation
You can visualize inheritance like this:
Animal (Base Class)
|
| inherits
Dog (Derived Class)
Animal → provides features
Dog → uses those features + adds its own
󹼧 Why Use Inheritance?
Here are some strong reasons:
1. Code Reusability
You don’t need to rewrite the same functions again.
2. Saves Time
Write once → use many times.
3. Better Structure
Programs become organized and easy to understand.
4. Extensibility
You can easily add new features in derived classes.
󹼧 Important Points to Remember
Base class = Parent
Easy2Siksha.com
Derived class = Child
Use : symbol to inherit
Access specifiers:
o public
o protected
o private
󹼧 Simple Everyday Example
Think of:
Vehicle → has speed, fuel
Car → inherits vehicle + has AC, music system
So instead of writing everything again, Car just inherits from Vehicle.
󹼧 Conclusion
Inheritance is one of the most powerful features of C++ that allows one class to reuse the
properties of another. It works just like real-life inheritance where children inherit qualities
from their parents.
In the example we saw, the Dog class inherits the eat() function from Animal, which saves
effort and makes the code clean and efficient.
8. How does the friend funcon work in C++?
Ans: 󷊆󷊇 What Is a Friend Function?
In C++, a friend function is a special function that is not a member of a class but is allowed
to access the private and protected members of that class.
Normally, private and protected members of a class are hidden from the outside world.
Only member functions of the class can access them. But sometimes, you want an external
function (or another class) to have special access. That’s where the friend function comes
in—it acts like a “trusted outsider.”
󷘧󷘨 Everyday Analogy
Easy2Siksha.com
Imagine a house with locked rooms. Only family members (class functions) can enter. But
you trust your best friend so much that you give them a spare key. They are not part of the
family, but they can still enter the private rooms.
That’s exactly how a friend function works—it’s not a member of the class, but it has special
access.
󼩺󼩻 Why Do We Need Friend Functions?
Friend functions are useful when:
1. Two classes need to work closely together and share private data.
2. Operator overloading requires access to private members.
3. You want to allow certain non-member functions to interact deeply with a class.
󹶓󹶔󹶕󹶖󹶗󹶘 How Do We Declare a Friend Function?
Inside the class, you declare the function with the keyword friend.
Syntax:
class ClassName {
friend returnType functionName(parameters);
};
This tells the compiler: “This function is not a member, but it can access private/protected
data.”
󺛺󺛻󺛿󺜀󺛼󺛽󺛾 Example 1: Simple Friend Function
#include <iostream>
using namespace std;
class Box {
private:
int length;
public:
Box(int l) {
length = l;
}
// Friend function declaration
friend void printLength(Box b);
};
// Friend function definition
void printLength(Box b) {
cout << "Length of box: " << b.length << endl;
Easy2Siksha.com
}
int main() {
Box b1(10);
printLength(b1); // Friend function can access private
member
return 0;
}
Here, printLength() is not a member of Box, but because it’s declared as a friend, it can
access the private variable length.
󺛺󺛻󺛿󺜀󺛼󺛽󺛾 Example 2: Friend Function Between Two Classes
#include <iostream>
using namespace std;
class ClassB; // Forward declaration
class ClassA {
private:
int valueA;
public:
ClassA(int v) : valueA(v) {}
// Declare friend function
friend void showValues(ClassA a, ClassB b);
};
class ClassB {
private:
int valueB;
public:
ClassB(int v) : valueB(v) {}
// Declare friend function
friend void showValues(ClassA a, ClassB b);
};
// Friend function definition
void showValues(ClassA a, ClassB b) {
cout << "ValueA: " << a.valueA << ", ValueB: " << b.valueB
<< endl;
}
int main() {
ClassA objA(5);
Easy2Siksha.com
ClassB objB(10);
showValues(objA, objB); // Accesses private members of
both classes
return 0;
}
Here, the friend function showValues() can access private members of both ClassA and
ClassB.
󺛺󺛻󺛿󺜀󺛼󺛽󺛾 Example 3: Operator Overloading with Friend Function
Friend functions are often used for operator overloading.
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// Friend function for operator +
friend Complex operator+(Complex c1, Complex c2);
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
// Friend function definition
Complex operator+(Complex c1, Complex c2) {
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2; // Uses friend function
c3.display(); // Output: 4 + 6i
return 0;
}
Here, the friend function operator+ can access private members real and imag to perform
addition.
󷗿󷘀󷘁󷘂󷘃 Diagram Explanation
Easy2Siksha.com
Think of it like this:
Class: Box
|
|---> Member functions (can access private data)
|
|---> Friend function (not a member, but trusted to access private data)
So the friend function stands outside the class but has a special “key” to enter.
󷘧󷘨 Relatable Story
Imagine you’re running a company. Employees (class members) can access company files.
But sometimes, you hire a consultant (friend function). The consultant is not an employee,
but you give them special access to files because they need to analyze data.
That’s how friend functions work—they are trusted outsiders who help when needed.
󼩏󼩐󼩑 Practice Exercise
Try writing a program with two classes: Student and Teacher.
Student has a private variable marks.
Teacher has a private variable name. Write a friend function that prints both the
teacher’s name and the student’s marks.
This will help you practice how friend functions can access private data across classes.
󷈷󷈸󷈹󷈺󷈻󷈼 Key Points to Remember
Friend functions are declared with the keyword friend.
They are not members of the class.
They can access private and protected members.
Useful for operator overloading and close cooperation between classes.
They break the strict encapsulation rule, so use them carefully.
󹵙󹵚󹵛󹵜 Conclusion
A friend function in C++ is a special function that, although not a member of a class, can
access its private and protected members. It is declared inside the class with the keyword
friend. Friend functions are useful for operator overloading, sharing data between classes,
and allowing trusted external functions to interact with private data.
For example, a friend function can add two complex numbers by accessing their private
members directly. In essence, friend functions act like “trusted outsiders”—not part of the
class family, but given special access when needed.
Easy2Siksha.com
This paper has been carefully prepared for educaonal purposes. If you noce any
mistakes or have suggesons, feel free to share your feedback.